package cn.exrick.xboot.modules.transport.controller;

import cn.exrick.xboot.common.exception.XbootException;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.RoleDTO;
import cn.exrick.xboot.modules.base.enums.RoleEnum;
import cn.exrick.xboot.modules.transport.param.QueryCompanyDepositDetailsParam;
import cn.exrick.xboot.modules.transport.service.CompanyDepositDetailsService;
import cn.exrick.xboot.modules.transport.vo.CompanyDepositDetailsVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gj
 */
@Slf4j
@RestController
@Api(tags = "物流公司抵押金明细信息管理接口")
@RequestMapping("/xboot/companyDepositDetails")
@Transactional
public class CompanyDepositDetailsController {

    @Autowired
    private CompanyDepositDetailsService companyDepositDetailsService;
    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<CompanyDepositDetailsVo> get(@PathVariable Long id) {
        return new ResultUtil<CompanyDepositDetailsVo>().setData(companyDepositDetailsService.getDepositDetailsById(id));
    }


    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<CompanyDepositDetailsVo>> getByCondition(QueryCompanyDepositDetailsParam param,
                                                                 PageVo page){
        RoleDTO currentRole = securityUtil.getCurrUser().getCurrentRole();
        if (currentRole == null) {
            throw new XbootException("当前用户无角色信息");
        }
        //平台用户 传入发单人id
        if(RoleEnum.PLATFORM_UER.getValue().equals(currentRole.getRoleName())){
            throw new XbootException("您无权执行此操作");
        }else if (currentRole.getRoleName().endsWith(RoleEnum.WAREHOUSE_ADMIN.getValue())){
            param.setWarehousesId(currentRole.getWarehouseId());
        }else if (RoleEnum.PLATFORM_ADMIN.getValue().equals(currentRole.getRoleName())){
            //
        }else {
            throw new XbootException("您无权执行此操作");
        }
        return new ResultUtil<IPage<CompanyDepositDetailsVo>>().setData(companyDepositDetailsService.getByCondition(PageUtil.initMpPage(page),param));
    }
}
